package com.esun.payment_api.service.impl;

import com.esun.common.utils.BlankUtils;
import com.esun.payment_api.bean.*;
import com.esun.payment_api.dao.InvFormDao;
import com.esun.payment_api.dao.InvFormDetail;
import com.esun.payment_api.domain.*;
import com.esun.payment_api.dto.*;
import com.esun.payment_api.mapper.DkClaimMapper;
import com.esun.payment_api.mapper.InvFormDetailMapper;
import com.esun.payment_api.mapper.InvFormListMapper;
import com.esun.payment_api.mapper.InvFormMapper;
import com.esun.payment_api.service.DkClaimService;
import com.esun.payment_api.service.InvFormService;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class InvFormServiceImpl implements InvFormService {

    private static final Logger LOG = LoggerFactory.getLogger(InvFormService.class);

    @Resource
    private InvFormDetailMapper invFormDetailMapper;

    @Resource
    private InvFormMapper invFormMapper;

    @Resource
    private InvFormListMapper invFormListMapper;

    @Resource
    private DkClaimMapper dkClaimMapper;


    @Resource
    private DkClaimService dkClaimService;

    @Override
    public InvFormBean getById(Integer id){
        return invFormMapper.getById(id);
    }

    @Override
    public int deleteById(Integer id) {
        return invFormMapper.deleteById(id);
    }

    @Override
    public int delete(List<Integer> ids){
        InvForm record = new InvForm();
        record.setIsDeleted("1");
        InvFormExample example = new InvFormExample();
        example.createCriteria().andIsDeletedEqualTo("0").andIdIn(ids);
        return invFormMapper.updateByExampleSelective(record,example);
    }

    @Override
    public int insert(InvForm invForm){
        return invFormMapper.insertSelective(invForm);
    }

    @Override
    public int insertReturnId(InvForm invForm){
        return invFormMapper.insertSelectiveReturnId(invForm);
    }

    @Override
    public int update(InvForm invForm){
        return invFormMapper.updateByPrimaryKeySelective(invForm);
    }


    @Override
    public List<InvForm> getList(InvFormQueryParam queryParam, Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return invFormDetailMapper.getList(queryParam);
    }

    @Override
    public List<InvFormDao> getListByType(InvFormQueryParam queryParam, Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return invFormListMapper.getListByType(queryParam);
    }

    @Override
    public String getSumMoneyBySearch(InvFormQueryParam queryParam) {
        return invFormListMapper.getSumMoneyBySearch(queryParam);
    }

    @Override
    public InvFormDetail getDetail(Integer id){
        return invFormDetailMapper.getDetail(id);
    }

    @Override
    public List<InvForm> getListByAny(String words, Integer pageSize, Integer pageNum){
        PageHelper.startPage(pageNum, pageSize);
        return invFormDetailMapper.getListByAny(words);
    }

    @Override
    public List<DkInvoiceBean> claimSelectInvoice(Short companyId, Integer claimId, Integer customerId, String searchStr, Integer pageSize, Integer pageNum){
        if (BlankUtils.isBlank(customerId)){
            DkClaim dkClaim = dkClaimMapper.selectByPrimaryKey(claimId);
            customerId = dkClaim.getCusId();
        }
        PageHelper.startPage(pageNum, pageSize);
        return invFormMapper.claimSelectInvoice(companyId,claimId,customerId,searchStr);
    }

    @Override
    public InvFormDetail getInvDetail(User user, Integer id) {
        return invFormDetailMapper.getInvDetail(user.getId(), id);
    }

    @Override
    public List<SysDictCustomer> getInvCustomer(Integer invId) {
        return invFormMapper.getInvCustomer(invId);
    }

    @Override
    public BigDecimal cancelPassedInv(PInvCancelParam param) {
        invFormMapper.cancelPassedInv(param);
        return param.getCancelId();
    }

    @Override
    public List<InvProcessBean> getInvProcess(String uuid) {
        List<InvProcessBean> applyList = invFormMapper.getInvApplicant(uuid);
        List<InvProcessBean> invProcessList = invFormMapper.getInvProcess(uuid);
        if (applyList.size() == 0) {
            System.out.println("获取发起人流程不成功");
        } else {
            applyList.addAll(invProcessList);
        }
        return applyList;
    }

    @Override
    public String getInvCancelUuid(String uuid) {
        return invFormMapper.getInvCancelUuid(uuid);
    }

    @Override
    public void invSubmitApproval(SubmitApprovalParam param) {
        String flowTypeName = "";
        if ("1".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程";
        } else if ("2".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(纵向)";
        } else if ("3".equals(param.getBusinessType())) {
            if (param.getDeptId() != (short) 90) {
                flowTypeName = "发票申请流程(预研-非研发处)";
            } else {
                flowTypeName = "发票申请流程(预研)";
            }
        } else if ("4".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(版面费)";
        } else if ("5".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(财务资金调拨)";
        } else if ("6".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(承兑汇票到期)";
        } else if ("7".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(民品(缝编))";
        } else if ("8".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(其他)";
        } else if ("9".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(工艺)";
        } else if ("10".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(民品)";
        } else if ("11".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(考试费)";
        } else if ("12".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(纵向YH)";
        } else if ("13".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(纵向WQ)";
        }
        invFormMapper.invSubmitApproval(param.getCoId(), param.getUuid(), param.getInvId(), param.getUserId(), param.getDeptId(), flowTypeName);
    }

    @Override
    public void invResubmitApproval(SubmitApprovalParam param) {
        String flowTypeName = "";
        if ("1".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程";
        } else if ("2".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(纵向)";
        } else if ("3".equals(param.getBusinessType())) {
            if (param.getDeptId() != (short) 90) {
                flowTypeName = "发票申请流程(预研-非研发处)";
            } else {
                flowTypeName = "发票申请流程(预研)";
            }
        } else if ("4".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(版面费)";
        } else if ("5".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(财务资金调拨)";
        } else if ("6".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(承兑汇票到期)";
        } else if ("7".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(民品(缝编))";
        } else if ("8".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(其他)";
        } else if ("9".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(工艺)";
        } else if ("10".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(民品)";
        } else if ("11".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(考试费)";
        } else if ("12".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(纵向YH)";
        } else if ("13".equals(param.getBusinessType())) {
            flowTypeName = "发票申请流程(纵向WQ)";
        }
        invFormMapper.invResubmitApproval(param.getCoId(), param.getUuid(), param.getInvId(), param.getUserId(), param.getDeptId(), flowTypeName);
    }

    @Override
    public void invProcessApproval(ProcessApprovalParam param) {
        invFormMapper.invProcessApproval(param.getStatus(), param.getFlowId(), param.getFlowNotes(), param.getUserId(), param.getInvId());
    }

    @Override
    public void cancelInvProcessApproval(ProcessApprovalParam param) {
        invFormMapper.cancelInvProcessApproval(param.getStatus(), param.getFlowId(), param.getFlowNotes(), param.getUserId(), param.getInvId());
    }

    @Override
    public String checkInvSubmit(Integer id) {
        return invFormMapper.checkInvSubmit(id);
    }

    @Override
    public int updateById(InvForm invForm) {
        return invFormMapper.updateByPrimaryKeySelective(invForm);
    }

    @Override
    public List<ApprovalNodeBean> getApprovalNodeList(Short coId) {
        return invFormMapper.getApprovalNodeList(coId);
    }

    @Override
    public BigDecimal invCopy(PInvCopyParam param) {
        invFormMapper.invCopy(param);
        return param.getNewInvId();
    }

    @Override
    public String getBusinessTypeByInvId(SubmitApprovalParam param) {
        return invFormMapper.getBusinessTypeByInvId(param.getCoId(), param.getInvId());
    }

    @Override
    public String checkInvDataAuth(Integer id, Short userId, Short deptId) {
        return invFormMapper.checkInvDataAuth(id, userId, deptId);
    }
}
